﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 5/7/2009
 * Time: 9:07 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace popLib
{
	/// <summary>
	/// Description of TransparentForm.
	/// </summary>
	public partial class TransparentForm : Form
	{
		Form child;		
		public bool Me_Active, Child_Active;
		TransparentFormOption options;
		public Bitmap bmpBackground;
		byte opacity;
		
		#region CONTRUCTOR
		public TransparentForm(Form child, Bitmap bitmap, byte opacity, TransparentFormOption options)
		{	
						
			this.child 				= child;
			this.options			= options;
			this.opacity			= opacity;
			
			child.BackgroundImage 	= null;
			if( options.AllowTransparentForm ){
				child.BackColor     = child.TransparencyKey = options.TransparentKey;
			}
			child.ShowInTaskbar   	= false;
			child.FormBorderStyle 	= FormBorderStyle.None;
			child.Owner				= this;
			child.FormClosed 		+= Handle_ChildFormClose;
			child.VisibleChanged 	+= Handle_ChildFormVisible;
			
			if( options.IsHandleOpacity ){
				child.Activated 		+= Handle_ChildFormActivated;
				child.Deactivate 		+= Handle_ChildFormDeactivate;
			}	        
	        
	        
	        //hide child window from ALT+TAB dialog
			APILib.SetWindowLong(child.Handle, (-20), ( APILib.GetWindowLong(child.Handle, (-20)) | 0x80) & ~0x40000);
								
			if( options.IsHandleOpacity ){
				this.Activated  += Form_Container_Activated;
				this.Deactivate += Form_Container_Deactivate;
			}			
			this.LocationChanged 	+= Form_Container_LocationChanged;
			
			
			this.Text 		   		= child.Text;
			this.StartPosition 		= child.StartPosition;
			this.Location      		= child.Location;
			this.FormBorderStyle 	= options.IsWindow ? FormBorderStyle.Fixed3D : FormBorderStyle.None;
			this.MaximizeBox		= false;
			this.MinimizeBox		= false;
			this.Size				= child.Size;
			this.ShowInTaskbar		= options.IsWindow;			
			this.ContextMenuStrip	= child.ContextMenuStrip;
			this.Visible			= child.Visible;
            this.Icon               = child.Icon;

            if (options.AllowMoveForm)
            {
                FormLib.moveFormEvent(this, options.StickBorderType);
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.Size = child.Size;
            }
			
			if( !options.FullContentSize ){
				this.Location  	= new Point( child.Location.X - options.Padding.Left, child.Location.Y - options.Padding.Top );
				this.Size		= new Size( this.Width + (options.Padding.Left + options.Padding.Right), this.Height + (options.Padding.Top + options.Padding.Bottom) );
			}
			
			if( options.Padding.All != 0 ) //resize bitmap to fix container form's size
				bitmap = resizeBackground( bitmap, options.Padding );
			FormLib.setTransparentBackground( this, bitmap, opacity );
			bmpBackground = bitmap;
						
			
			
			//hide child window from ALT+TAB dialog
			if( !options.IsWindow ) APILib.SetWindowLong(this.Handle, (-20), ( APILib.GetWindowLong(this.Handle, (-20)) | 0x80) & ~0x40000);
						
		}
		#endregion
									
		#region Events
		private void Handle_ChildFormClose(object sender, FormClosedEventArgs e){
	        this.Dispose();
	    }
		
		private void Handle_ChildFormVisible(object sender, EventArgs e){
	        this.Visible = child.Visible;
	    }
	
	    private void Handle_ChildFormActivated(object sender, EventArgs e){
	        Child_Active = true;	        
	        if( options.IsHandleOpacity && child != null ) child.Opacity = 1;
	    }
	
	    private void Handle_ChildFormDeactivate(object sender, EventArgs e){
	        Child_Active = false;
		    if (!Me_Active && options.IsHandleOpacity && child != null){	           
	            child.Opacity = 0.9;
			}
		}
		
		private void Form_Container_Activated(object sender, EventArgs e){
	        Me_Active = true;	        
	        if( options.IsHandleOpacity ) child.Opacity = 1;
	    }

		private void Form_Container_Deactivate(object sender, EventArgs e){
	        Me_Active = false;
	        if (!Child_Active && options.IsHandleOpacity ) {
	            child.Opacity = 0.9;
	       }
	    }

		
		private void Form_Container_LocationChanged(object sender, EventArgs e){
			if( options.FullContentSize ) child.Location = new Point(this.Location.X, this.Location.Y);
			else child.Location = new Point(this.Location.X + options.Padding.Left, this.Location.Y + options.Padding.Top);
	    }
		#endregion
	
		#region override parameters

		protected override CreateParams CreateParams
		{
			get 
			{
				CreateParams cp = base.CreateParams;
				cp.ExStyle = cp.ExStyle | 0x00080000; //WS_EX_LAYERED				
				return cp;
			}
		}
		#endregion
		
		#region Procedures		
		
		public void reDrawBackground(){
			FormLib.setTransparentBackground( this, bmpBackground, opacity );
		}
		
		public void reDrawBackground(Bitmap background){
			FormLib.setTransparentBackground( this, background, opacity );
		}
		
		private Bitmap resizeBackground(Bitmap bitmap, Padding region){
	        						
	        //L15, R15, T32, B19
	        Bitmap MyBitmap = ImageLib.ResizePadding( bitmap, region, this.Size );
	        Graphics Gr = Graphics.FromImage(MyBitmap);
	        
	        if( options.ShowTitle ){
	        	Rectangle IconRect = new Rectangle(region.Left, 10, 18, 18);
	        	Gr.DrawImage( child.Icon.ToBitmap(), IconRect );
	        	
		        Rectangle TextRect = new Rectangle(region.Left + 23, 8, this.Width - (region.Left+region.Right+ 23), 23);
		        Gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
		        StringFormat SF = new StringFormat();
		        
		        SF.Alignment = StringAlignment.Near;
		        SF.LineAlignment = StringAlignment.Center;
		        Gr.DrawString(child.Text, new Font("Tahoma", 8, FontStyle.Bold), new SolidBrush(options.TitleColor), TextRect, SF);
	        }
	        return MyBitmap;
		}
		#endregion
				
	}
	
	#region class TransparentFormOption
	public class TransparentFormOption{
		Padding padding			= new Padding(0); //no resize form, follow bitmap size
		bool fullContentSize 	= true; //content-form's size fit to container-form, false=> content inside padding
		bool showTitle 			= true;
		bool isWindow			= true; //is window show in taskbar(minimum)
		bool isHandleOpacity	= true; //is change opacity automatically		
		bool allowMoveForm		= true;
		bool allowTransparentForm	= true;
		Color titleColor 		= Color.FromArgb(50, 50, 50);
		Color transparentKey	= Color.Fuchsia;
		StickBorderType stickBorderType = StickBorderType.None;
		public TransparentFormOption(){}
		
		public TransparentFormOption( Padding padding, bool fullContentSize, bool isWindow, bool showTitle, Color titleColor, bool isHandleOpacity, bool allowMoveForm, StickBorderType stickBorderType, bool allowTransparentForm, Color transparentKey ){
			this.padding 			= padding;
			this.fullContentSize 	= fullContentSize;
			this.isWindow			= isWindow;
			this.showTitle 			= showTitle;
			this.titleColor 		= titleColor;
			this.isHandleOpacity	= isHandleOpacity;
			this.allowMoveForm		= allowMoveForm;
			this.stickBorderType 	= stickBorderType;
			this.allowTransparentForm 	= allowTransparentForm;
			this.transparentKey		= transparentKey;
		}
		
		public TransparentFormOption( Padding padding, bool fullContentSize ){
			this.padding 			= padding;
			this.fullContentSize 	= fullContentSize;
		}
		
		public TransparentFormOption( Padding padding, bool fullContentSize, bool isWindow ){
			this.padding 			= padding;
			this.fullContentSize 	= fullContentSize;
			this.isWindow			= isWindow;
		}
		
		public Padding Padding{ get{return padding;} set{padding = value;} }
		public bool FullContentSize{ get{return fullContentSize;} set{fullContentSize = value;} }
		public bool ShowTitle{ get{return showTitle;} set{showTitle = value;} }
		public bool IsWindow{ get{return isWindow;} set{isWindow = value;} }
		public bool IsHandleOpacity{ get{return isHandleOpacity;} set{isHandleOpacity = value;} }
		public bool AllowMoveForm{ get{return allowMoveForm;} set{allowMoveForm = value;} }
		public bool AllowTransparentForm{ get{return allowTransparentForm;} set{ FullContentSize = allowTransparentForm = value; } }
		public Color TitleColor{ get{return titleColor;} set{titleColor = value;} }
		public Color TransparentKey{ get{return transparentKey;} set{transparentKey = value;} }
		public StickBorderType StickBorderType{ get{return stickBorderType;} set{stickBorderType = value;} }
	}
	#endregion
}
